﻿using BaseDal;
using Models;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaseBll
{
    public class DeptBll
    {
        private static readonly Lazy<DeptBll> bll = new Lazy<DeptBll>(() => new DeptBll());
        public static DeptBll Instance { get; } = bll.Value;

        private DeptBll()
        { }

        /// <summary>
        /// 查询数据是否存在
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public bool IsExist(string strWhere)
        {
            return DeptDal.Instance.IsExist(strWhere);
        }

        /// <summary>
        /// 查询list
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public List<Department> GetListModels(string strWhere, string filedOrder)
        {
            return DeptDal.Instance.GetListModels(strWhere, filedOrder);
        }

        /// <summary>
        /// 将list转为树形json结构
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public JArray GetTreeJson(List<Department> list)
        {
            JArray result = new JArray();
            List<Department> parentList = GetParentNodes(list);
            JArray children;
            JObject parent;

            foreach (Department model in parentList)
            {
                children = GetChilds(list, model.ID);
                parent = new JObject
                {
                    ["id"] = model.ID,
                    ["fullName"] = model.FullName,
                    ["parentId"] = model.ParentId,
                    ["status"] = model.Status,
                    ["category"] = model.Category,
                    ["createTime"] = model.CreateTime,
                    ["head"] = model.Head,
                    ["phone"] = model.Phone
                };
                if (children.Count > 0)
                {
                    //parent["hasChildren"] = true;
                    parent["children"] = children;
                }

                result.Add(parent);
            }
            return result;
        }

        /// <summary>
        /// 获取父节点信息
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public List<Department> GetParentNodes(List<Department> list)
        {
            List<Department> result = new List<Department>();
            result.AddRange(list.FindAll(a => a.ParentId == 0));
            return result;
        }

        /// <summary>
        /// 子节点数据
        /// </summary>
        /// <param name="list">列表数据</param>
        /// <param name="id">id</param>
        /// <returns></returns>
        public JArray GetChilds(List<Department> list, int parentId)
        {
            JArray result = new JArray();
            foreach (Department model in list)
            {
                if (model.ParentId == parentId)
                {
                    JObject obj = new JObject();
                    JArray children = GetChilds(list, model.ID);
                    obj["id"] = model.ID;
                    obj["fullName"] = model.FullName;
                    obj["parentId"] = model.ParentId;
                    obj["status"] = model.Status;
                    obj["category"] = model.Category;
                    obj["createTime"] = model.CreateTime;
                    obj["head"] = model.Head;
                    obj["phone"] = model.Phone;
                    if (children.Count > 0)
                    {
                        //obj["hasChildren"] = true;
                        obj["children"] = children;
                    }

                    result.Add(obj);
                }
            }
            return result;
        }

        /// <summary>
        /// 根据id查询实体类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Department GetModelById(int id)
        {
            return DeptDal.Instance.GetModelById(id);
        }

        /// <summary>
        /// 新增部门
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddDept(Department model)
        {
            return DeptDal.Instance.AddDept(model);
        }

        /// <summary>
        /// 修改部门
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateDept(Department model)
        {
            return DeptDal.Instance.UpdateDept(model);
        }

        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeleteDept(int id, string userName)
        {
            return DeptDal.Instance.DeleteDept(id, userName);
        }

        /// <summary>
        /// 修改状态（启用、禁用）
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public int UpdateStatus(int id, int status, string userName)
        {
            return DeptDal.Instance.UpdateStatus(id, status, userName);
        }

        /// <summary>
        /// 验证model各字段合法性
        /// </summary>
        /// <param name="model"></param>
        /// <param name="msg">错误信息</param>
        /// <returns></returns>
        public bool VerifyModel(Department model, ref string msg)
        {
            if (model.ParentId < 0)
            {
                model.ParentId = 0;
            }
            if (string.IsNullOrEmpty(model.Category))
            {
                msg = "请选择部门类型";
                return false;
            }
            //if (model.Category.Equals("Company"))
            //{
            //    if (model.ParentId > 0)
            //    {
            //        msg = "公司不能存在上级";
            //        return false;
            //    }
            //}
            if (string.IsNullOrEmpty(model.FullName))
            {
                msg = "名称不能为空";
                return false;
            }
            string strWhere = "";
            if (model.ID > 0)
            {
                // 公司名称不能重复（部门可以）
                if (model.ParentId == 0)
                {
                    strWhere = " and FullName='" + model.FullName + "' and ID !=" + model.ID;
                }
            }
            else
            {
                // 公司名称不能重复（部门可以）
                if (model.ParentId == 0)
                {
                    strWhere = " and FullName='" + model.FullName + "'";
                }
            }
            if (!string.IsNullOrEmpty(strWhere))
            {
                if (IsExist(strWhere))
                {
                    msg = "公司名称已存在";
                    return false;
                }
            }

            return true;
        }
    }
}